package com.boolib.http.core;

import com.boolib.http.core.defaults.DefaultHttpChain;
import com.boolib.http.core.face.handler.RequestHandler;
import com.boolib.http.core.util.ClassNameUtil;
import com.boolib.http.core.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @author wfq
 */
@Slf4j
@ComponentScan({"com.boolib.http"})
@Configuration
public class HttpCoreAutoConfiguration {

    @Autowired
    private DefaultHttpChain defaultHttpChain;


    @PostConstruct
    public void spiLoad(){


        log.info("spi load requestHandler");

        ServiceLoader<RequestHandler> load = ServiceLoader.load(RequestHandler.class);

        Iterator<RequestHandler> iterator = load.iterator();

        int count = 0 ;

        while (iterator.hasNext()) {
            count++;
            RequestHandler next = iterator.next();
            String simpleName = next.getClass().getSimpleName();
            RequestHandler bean = SpringUtil.getBean(ClassNameUtil.toLowerCaseFirstOne(simpleName));
            defaultHttpChain.add(bean);
        }

        log.info("一共加载了 {}个RequestHandler",count);

    }
}
